//
//  MusicViewController.m
//  DolphinMusic
//
//  Created by dlios on 15-3-23.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "MusicViewController.h"
#import "AFNetworking.h"

@interface MusicViewController ()

@property(nonatomic , retain)UILabel *titlelabel;
@property(nonatomic , retain)UILabel *authorlabel;
@property(nonatomic , assign)BOOL tap;
@property(nonatomic , retain)UILabel *writelabel;
@property(nonatomic , retain)UISlider *showSlider;
@property(nonatomic , retain)UILabel *startTime;
@property(nonatomic , retain)UILabel *stopTime;
@property(nonatomic , retain)NSTimer *timer;
@property(nonatomic , retain)UIScrollView *writescroll;

@property(nonatomic , retain)UISlider *VolumeSlider;



@end

@implementation MusicViewController
- (void)dealloc
{
    [_VolumeSlider release];
    [_titlelabel release];
    [_authorlabel release];
    [_writelabel release];
    [_showSlider release];
    [_startTime release];
    [_stopTime release];
    [_timer release];
    [_writescroll release];
    [_musicimage release];
    [_musiclisten release];
    [_songID release];
    [_streamer release];
    [super dealloc];
}


- (void)Musiclisten
{
    NSString *str = [NSString stringWithFormat:@"http://tingapi.ting.baidu.com/v1/restserver/ting?from=webapp_music&method=baidu.ting.song.play&format=json&callback=&songid=%@&_=1413017198449",self.songID];
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dicc = responseObject;
        NSDictionary *dict = [dicc objectForKey:@"songinfo"];

        MusicListen *musiclisten = [[MusicListen alloc]initWithDictionary:dict];
        
        self.titlelabel.text = musiclisten.title;
        self.authorlabel.text = musiclisten.author;
        
        

        NSString *str1 = musiclisten.pic_huge;
        str1 = [str1 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSURL *urll = [NSURL URLWithString:str1];
        NSMutableURLRequest *requ = [NSMutableURLRequest requestWithURL:urll cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20];
        requ.HTTPMethod = @"GET";
        [NSURLConnection sendAsynchronousRequest:requ queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
            
            self.musicimage.image = [UIImage imageWithData:data];
        
        }];
        
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"中央图片出错了");
    }];
    
    // ******************歌词*******************
    NSString *write = [NSString stringWithFormat:@"http://tingapi.ting.baidu.com/v1/restserver/ting?from=webapp_music&method=baidu.ting.song.lry&format=json&callback=&songid=%@&_=1413017198449",self.songID];
    [manager GET:write parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        
        self.writelabel.text = [[dic objectForKey:@"lrcContent"]stringByReplacingOccurrencesOfString:@"["  withString:@""];
        self.writelabel.text = [self.writelabel.text stringByReplacingOccurrencesOfString:@"]"  withString:@""];
        self.writelabel.text = [self.writelabel.text stringByReplacingOccurrencesOfString:@"0"  withString:@""];
        self.writelabel.text = [self.writelabel.text stringByReplacingOccurrencesOfString:@"1"  withString:@""];
        self.writelabel.text = [self.writelabel.text stringByReplacingOccurrencesOfString:@"."  withString:@""];
        self.writelabel.text = [self.writelabel.text stringByReplacingOccurrencesOfString:@"2"  withString:@""];
        self.writelabel.text = [self.writelabel.text stringByReplacingOccurrencesOfString:@"3"  withString:@""];
        self.writelabel.text = [self.writelabel.text stringByReplacingOccurrencesOfString:@"4"  withString:@""];
        self.writelabel.text = [self.writelabel.text stringByReplacingOccurrencesOfString:@"5"  withString:@""];
        self.writelabel.text = [self.writelabel.text stringByReplacingOccurrencesOfString:@"6"  withString:@""];
        self.writelabel.text = [self.writelabel.text stringByReplacingOccurrencesOfString:@"7"  withString:@""];
        self.writelabel.text = [self.writelabel.text stringByReplacingOccurrencesOfString:@"8"  withString:@""];
        self.writelabel.text = [self.writelabel.text stringByReplacingOccurrencesOfString:@"9"  withString:@""];
        self.writelabel.text = [self.writelabel.text stringByReplacingOccurrencesOfString:@":"  withString:@""];
        self.writelabel.text = [self.writelabel.text stringByReplacingOccurrencesOfString:@"匹配时间为  分  秒  的歌曲"  withString:@""];
        [self write];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"歌词出错");
    }];

  
}
// 歌词自适应高度
- (void)write
{
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
    CGRect rect = [self.writelabel.text boundingRectWithSize:CGSizeMake(self.writescroll.frame.size.width, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    self.writelabel.frame = CGRectMake(0, 0, self.writescroll.frame.size.width, rect.size.height);

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    [self Musiclisten];
//    
//    
//    self.view.backgroundColor = [UIColor grayColor];
//    
//    // 返回图标
//    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 29, 30, 30)];
//    imageView.image = [UIImage imageNamed:@"left.png"];
//    imageView.userInteractionEnabled = YES;
//    [self.view addSubview:imageView];
//    [imageView release];
//    
//    // 返回按钮
//    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
//    button.frame = CGRectMake(0, 0, 35, 35);
//    [button setTitle:@"" forState:UIControlStateNormal];
//    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
//    [imageView addSubview:button];
//    
//    // 中心图片
//    self.musicimage = [[UIImageView alloc]initWithFrame:CGRectMake(10, 95, self.view.frame.size.width - 10 * 2, self.view.frame.size.width - 10 * 2)];
//    self.musicimage.layer.cornerRadius = 20;
//    self.musicimage.layer.masksToBounds = YES;
//    [self.view addSubview:self.musicimage];
//    [_musicimage release];
//
//    // 曲歌名label
//    self.titlelabel = [[UILabel alloc]initWithFrame:CGRectMake(55, 23, self.view.frame.size.width - 55, 40)];
//    self.titlelabel.font = [UIFont boldSystemFontOfSize:24];
//    [self.view addSubview:self.titlelabel];
//    [_titlelabel release];
//    
//    // 歌手名label
//    self.authorlabel = [[UILabel alloc]initWithFrame:CGRectMake(55,55, self.view.frame.size.width - 55, 40)];
//    [self.view addSubview:self.authorlabel];
//    [_authorlabel release];
//    
//    
//    // 暂停按钮
//    UIImageView *imageview1 = [[UIImageView alloc]initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 25, self.view.frame.size.height - 55, 50, 50)];
//    imageview1.image = [UIImage imageNamed:@"stop.png"];
//    imageview1.userInteractionEnabled = YES;
//    [self.view addSubview:imageview1];
//    [imageview1 release];
//    
//    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
//    tapGesture.numberOfTapsRequired = 1;
//    [imageview1 addGestureRecognizer:tapGesture];
//    [tapGesture release];
//    
//    
//    // 歌曲进度条
//    self.showSlider = [[UISlider alloc] initWithFrame:CGRectMake(10, self.view.frame.size.height - 95, self.view.frame.size.width - 20, 10)];
//    [self.showSlider setThumbImage:[UIImage imageNamed:@"drop.png"] forState:UIControlStateNormal];
//    self.showSlider.backgroundColor = [UIColor clearColor];
//    [self.view addSubview:self.showSlider];
//    [self.showSlider addTarget:self action:@selector(seekSlider:) forControlEvents:UIControlEventValueChanged];
//    [_showSlider release];
//    
//    // 歌曲开始的时间
//    self.startTime = [[UILabel alloc] initWithFrame:CGRectMake(20, self.view.frame.size.height - 67, 100, 10)];
//    self.startTime.backgroundColor = [UIColor clearColor];
//    self.startTime.font = [UIFont systemFontOfSize:10];
//    [self.view addSubview:self.startTime];
//    [_startTime release];
//    
//    // 歌曲结束的时间
//    self.stopTime = [[UILabel alloc] initWithFrame:CGRectMake(275, self.view.frame.size.height - 67, 100, 10)];
//    self.stopTime.backgroundColor = [UIColor clearColor];
//    self.stopTime.textAlignment = NSTextAlignmentCenter;
//    self.stopTime.font = [UIFont systemFontOfSize:10];
//    [self.view addSubview:self.stopTime];
//    [_stopTime release];
//    
////    //调整音量的slider
////    self.VolumeSlider = [[UISlider alloc] initWithFrame:CGRectMake(275, 55, 100, 10)];
////    [self.VolumeSlider setThumbImage:[UIImage imageNamed:@"drop.png"] forState:UIControlStateNormal];
////    self.VolumeSlider.backgroundColor = [UIColor clearColor];
////    self.VolumeSlider.maximumValue = 1;
////    [self.view addSubview:self.VolumeSlider];
//    [self.VolumeSlider addTarget:self action:@selector(volumeSlider:) forControlEvents:UIControlEventValueChanged];
//

    // 歌词背景滑动
//    self.writescroll = [[UIScrollView alloc]initWithFrame:CGRectMake(10, 450, 355, 120)];
//    //self.writescroll.contentOffset = CGPointMake(0, self.showSlider.value);
//    //NSLog(@"%f",self.showSlider.value);
//    self.writescroll.contentSize = CGSizeMake(0, 2000);
//    [self.view addSubview:self.writescroll];
//    [_writescroll release];
//    
//    
//    // 歌词
//    self.writelabel = [[UILabel alloc]init];
//    //self.writelabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 355, 130)];
//    self.writelabel.numberOfLines = 0;
//    self.writelabel.textColor = [UIColor cyanColor];
//    [self.writescroll addSubview:self.writelabel];
//    [_writelabel release];
//
//    
//    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.f target:self selector:@selector(track) userInfo:nil repeats:YES];
}


//
//- (void)volumeSlider:(UISlider *)sender {
//    UISlider *slider = sender;
//    [_isplayer setVolume:slider.value];
//}
//
// 进度条改变播放的东西
////- (void)seekSlider:(UISlider *)sender {
////    
////    if ([self.streamer isPlaying]) {
////        UISlider *processSlider = sender;
////        [self.streamer seekToTime:processSlider.value];    
////        self.writescroll.contentOffset = CGPointMake(0,processSlider.value * 2);
////        }
////    }
//
//
//// 更新时间
//- (void)track
//{
//    
//    if (self.showSlider) {
//        
//        self.showSlider.maximumValue = self.streamer.duration;
//        self.showSlider.value = self.streamer.progress;
//    
//        NSInteger proMin = (NSInteger)self.streamer.progress / 60;
//        NSInteger proSec = (NSInteger)self.streamer.progress % 60;
//        
//        NSInteger durMin = (NSInteger)self.streamer.duration / 60;
//        NSInteger durSec = (NSInteger)self.streamer.duration % 60;
//        
//        self.startTime.text = [NSString stringWithFormat:@"%02ld:%02ld", proMin, proSec];
//        self.stopTime.text = [NSString stringWithFormat:@"%02ld:%02ld", durMin, durSec];
//    
//        
//        
//    }
//}
//
//// 暂停方法
//- (void)tapAction:(UITapGestureRecognizer *)tap
//{
//    if (self.tap == YES) {
//         [self.streamer start];
//    }if (self.tap == NO) {
//       [self.streamer pause];
//    }
//    self.tap =! self.tap ;
//}
//
//
//// 返回上一页
//- (void)buttonAction:(UIButton *)button
//{
//    [self.navigationController popToRootViewControllerAnimated:YES];
//    
//}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
